package com.cloud.sample.bean;

import com.cloud.common.util.DateTimeUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.Date;

/**
 * 孕期信息
 *
 * @author fengwenjin
 * @since 2021/7/10
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GestationInfo {
    /**
     * 总天数
     */
    private int totalDays;

    /**
     * 孕周
     */
    private int totalWeeks;

    /**
     * 孕周余天
     */
    private int weekDays;

    /**
     * 状态，1孕早期 2孕中期 3孕晚期
     */
    private int status;

    /**
     * 根据预产期返回孕期信息
     *
     * @param expectedDate
     * @return GestationInfo
     */
    public static GestationInfo of(Date expectedDate) {
        if (expectedDate == null || expectedDate.before(new Date())) {
            return null;
        }
        // 预产期为280天，所以往前推算出怀孕时间点
        LocalDate pregnancyDate = DateTimeUtils.asLocalDate(expectedDate).plusDays(-280);
        // 怀孕天数
        int totalDays = (int) (LocalDate.now().toEpochDay() - pregnancyDate.toEpochDay());
        // 计算孕周
        int totalWeeks = totalDays / 7;
        int weekDays = totalDays % 7;
        // 计算状态
        int status;
        if (totalWeeks >= 29) {
            status = 3;
        } else if (totalWeeks >= 13 && totalWeeks <= 28) {
            status = 2;
        } else {
            status = 1;
        }
        return new GestationInfo(totalDays, totalWeeks, weekDays, status);
    }
}
